// pages/shop/destination/destination.ts
import { doList, doDel } from '~/api/destination'
let isRefresh = false
interface queryFormSate {
  roadLineCustomerId: string
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    queryForm: {
      roadLineCustomerId: '',
    } as queryFormSate,
    status: 'loading',
    tableData: [] as any[],
    dialogKey: 'showConfirm' as string,
    showConfirm: false,
    oldIndex: 0
  },
  handOpen(this: any, e: any) {
    const dataset = e.currentTarget.dataset
    if (dataset.n === -1) {
      this.formDia.open(this.data.queryForm)
    } else {
      const info = this.data.tableData[dataset.n]
      this.formDia.open(info)
    }
  },
  handDel(e: any) {
    const dataset = e.currentTarget.dataset
    this.setData({
      showConfirm: true,
      oldIndex: dataset.n
    })
  },
  closeDialog(this: any) {
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
  },
  async handConfirm() {
    const { tableData, oldIndex } = this.data
    const info = tableData[oldIndex]
    await doDel({
      id: info.id
    })
    wx.showToast({
      icon: 'none',
      title: '删除成功'
    })
    tableData.splice(oldIndex, 1)
    this.closeDialog()
    this.setData({
      tableData
    })
  },
  async fetchData() {
    this.setData({
      status: 'loading'
    })
    const { queryForm } = this.data
    const data = await doList(queryForm)
    this.setData({
      tableData: data,
      queryForm,
      status: 'nomore'
    })
    if (isRefresh) {
      wx.stopPullDownRefresh();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option: any) {
    this.setData({
      queryForm: {
        roadLineCustomerId: option.id
      }
    })
    this.fetchData()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady(this: any) {
    this.formDia = this.selectComponent("#formDia");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    isRefresh = true
    const { queryForm } = this.data
    this.setData({
      status: 'loading',
      tableData: [],
      queryForm
    })
    this.fetchData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})